package com.lxd.sunso.product.service.impl;

import com.lxd.sunso.common.utils.PageUtils;
import com.lxd.sunso.common.utils.Query;
import com.lxd.sunso.product.entity.MachineEntity;
import com.lxd.sunso.product.entity.UserEntity;
import com.lxd.sunso.product.service.MachineService;
import com.lxd.sunso.product.service.UserService;
import com.lxd.sunso.product.vo.CustomerVo;
import com.lxd.sunso.product.vo.MachineNameAndIdVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxd.sunso.product.dao.CustomerDao;
import com.lxd.sunso.product.entity.CustomerEntity;
import com.lxd.sunso.product.service.CustomerService;


@Service("customerService")
public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity> implements CustomerService {
    @Autowired
    private MachineService machineService;

    @Autowired
    private UserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CustomerEntity> page = this.page(
                new Query<CustomerEntity>().getPage(params),
                new QueryWrapper<CustomerEntity>()
        );

        addMachineStatu(page);
        return new PageUtils(page);
    }

    private void addMachineStatu(IPage<CustomerEntity> page) {
        List<CustomerEntity> customerEntities = page.getRecords();
        List<CustomerEntity> collect = customerEntities.stream().map(customerEntity -> {
            Long id = customerEntity.getId();
            String status = getStatus(id);
            customerEntity.setMachineStatu(status);
            return customerEntity;
        }).collect(Collectors.toList());
        page.setRecords(collect);
    }

    @Override
    public PageUtils search(Map<String, Object> params) {
        IPage<CustomerEntity> page = null;
        String key = (String) params.get("key");
        String area = (String) params.get("area");
        QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.like("CustomerName", key);

        }

        if (!StringUtils.isEmpty(area)) {
            String[] strings = area.split(",");
            ArrayList<String> arrayList = new ArrayList<>();
            for (String s : strings) {
                arrayList.add(s);
            }
            queryWrapper.in("Area", arrayList);
        }


        page = this.page(
                new Query<CustomerEntity>().getPage(params),
                queryWrapper);

        List<CustomerEntity> customerEntities = page.getRecords();
        List<CustomerEntity> collect = customerEntities.stream().map(customerEntity -> {
            Long id = customerEntity.getId();
            String status = getStatus(id);
            customerEntity.setMachineStatu(status);
            return customerEntity;
        }).collect(Collectors.toList());

        page.setRecords(collect);
        return new PageUtils(page);
    }

    /**
     * 删除顾客以及对应的用户以及设备
     *
     * @param asList
     */
    @Override
    public void removeAll(List<Integer> asList) {
        for (Integer customerId : asList) {
            userService.remove(new QueryWrapper<UserEntity>().eq("UserID", customerId));
            List<MachineEntity> entities = machineService.list(new QueryWrapper<MachineEntity>().eq("CustomerID", customerId));
            if (entities != null && entities.size() > 0) {
                List<Long> machineIds = entities.stream().map(entity -> {
                    return entity.getId();
                }).collect(Collectors.toList());
                if (machineIds != null && machineIds.size() > 0) {
                    machineService.delete(machineIds);
                }
            }
        }
        this.removeByIds(asList);
    }

    //新增客户，保证唯一性
    @Override
    public boolean saveCustomer(CustomerEntity customer) {
        CustomerEntity customerEntity = this.getOne(new QueryWrapper<CustomerEntity>().eq("CustomerName", customer.getCustomername()));
        if (customerEntity != null) {
            return false;
        } else {
            this.save(customer);
            return true;
        }
    }

    //获取顾客的名称和ID
    @Override
    public List<CustomerVo> getCusNameAndId() {
        List<CustomerEntity> list = this.list();
        List<CustomerVo> collect = list.stream().map(entity -> {
            CustomerVo customerVo = new CustomerVo();
            BeanUtils.copyProperties(entity, customerVo);
            return customerVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<MachineNameAndIdVo> getmachineByCustomer(String customerName) {
        CustomerEntity customerEntity = this.getOne(new QueryWrapper<CustomerEntity>().eq("customerName", customerName));
        List<MachineEntity> entities = machineService.list(new QueryWrapper<MachineEntity>().eq("customerId", customerEntity.getId()));
        List<MachineNameAndIdVo> collect = entities.stream().map(item -> {
            MachineNameAndIdVo machineNameAndIdVo = new MachineNameAndIdVo();
            BeanUtils.copyProperties(item, machineNameAndIdVo);
            return machineNameAndIdVo;
        }).collect(Collectors.toList());
        return collect;
    }


    private String getStatus(Long id) {
        Long onNum = machineService.queryOnByCustomer(id);
        Long num = machineService.queryNumByCustomer(id);
        return (onNum + "/" + num);
    }
}